###########################################################
### Replication Code for Balance Graphs                 ###
### Title: Relative Gains in the Shadow of a Trade War  ###
### Authors: Eddy S. F. Yeung and Kai Quek              ###
### Version: December 6, 2021                           ###
###########################################################

### Set-up ###
## Clean the R environment and set the working directory
# RStudio version: 1.3.959
rm(list = ls())
setwd("~/Downloads/trade_replication") # change to your own working directory

## Load the required packages
library(gplots)    # version 3.1.1
library(extrafont) # version 0.17
# windowsFonts("Times" = windowsFont("Times")) # may be required for Windows users

## Import the main dataset that contains all survey responses
df <- read.csv("trade.csv")

### Figure A2: Univariate Balance on Pre-Treatment Covariates ###
pdf("Figure_balance.pdf", height = 11, width = 11)
par(mfrow = c(3,3))

# Age
plot(density(df[df$exp_group==1,]$age, na.rm=T, bw=4), family="Times", xlab="Age", ylim=c(0,0.04), xlim=c(10,90), main="Age", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$age, na.rm=T, bw=4), xlab="", ylim=c(0,0.04), xlim=c(10,90), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$age, na.rm=T, bw=4), xlab="", ylim=c(0,0.04), xlim=c(10,90), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$age, na.rm=T, bw=4), xlab="", ylim=c(0,0.04), xlim=c(10,90), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$age, na.rm=T, bw=4), xlab="", ylim=c(0,0.04), xlim=c(10,90), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$age, na.rm=T, bw=4), xlab="", ylim=c(0,0.04), xlim=c(10,90), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$age, na.rm=T, bw=4), xlab="", ylim=c(0,0.04), xlim=c(10,90), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$age, na.rm=T, bw=4), xlab="", ylim=c(0,0.04), xlim=c(10,90), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topright", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Gender
plot(density(df[df$exp_group==1,]$female, na.rm=T, bw=0.1), xlab="Gender (0 = Male, 1 = Female)", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="Gender", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$female, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$female, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$female, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$female, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$female, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$female, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$female, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.5), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topleft", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Race
plot(density(df[df$exp_group==1,]$white, na.rm=T, bw=0.1), xlab="White (0 = No, 1 = Yes)", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="Race", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$white, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$white, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$white, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$white, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$white, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$white, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$white, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.2), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topleft", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Household income
df$income3 <- ifelse((df$income==7 | df$income==8), 1, 0)
df$income3 <- ifelse((df$income==9 | df$income==10 | df$income==11 | df$income==12 | df$income==13 | df$income==14), 2, df$income3)
plot(density(df[df$exp_group==1,]$income3, na.rm=T, bw=0.25), xlab="Household income (1 = At medium income, 2 = Above, 0 = Below)", ylim=c(0,1.2), xlim=c(-1,3), main="Household income", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$income3, na.rm=T, bw=0.25), xlab="", ylim=c(0,1.2), xlim=c(-1,3), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$income3, na.rm=T, bw=0.25), xlab="", ylim=c(0,1.2), xlim=c(-1,3), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$income3, na.rm=T, bw=0.25), xlab="", ylim=c(0,1.2), xlim=c(-1,3), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$income3, na.rm=T, bw=0.25), xlab="", ylim=c(0,1.2), xlim=c(-1,3), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$income3, na.rm=T, bw=0.25), xlab="", ylim=c(0,1.2), xlim=c(-1,3), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$income3, na.rm=T, bw=0.25), xlab="", ylim=c(0,1.2), xlim=c(-1,3), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$income3, na.rm=T, bw=0.25), xlab="", ylim=c(0,1.2), xlim=c(-1,3), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topleft", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Education
plot(density(df[df$exp_group==1,]$college, na.rm=T, bw=0.1), xlab="College degree (0 = No, 1 = Yes)", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="College degree", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$college, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$college, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$college, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$college, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$college, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$college, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$college, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.7), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topleft", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Party ID
plot(density(df[df$exp_group==1,]$republic, na.rm=T, bw=0.1), xlab="Republican (0 = No, 1 = Yes)", ylim=c(0,3), xlim=c(-0.4,1.4), main="Party affiliation", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$republic, na.rm=T, bw=0.1), xlab="", ylim=c(0,3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$republic, na.rm=T, bw=0.1), xlab="", ylim=c(0,3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$republic, na.rm=T, bw=0.1), xlab="", ylim=c(0,3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$republic, na.rm=T, bw=0.1), xlab="", ylim=c(0,3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$republic, na.rm=T, bw=0.1), xlab="", ylim=c(0,3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$republic, na.rm=T, bw=0.1), xlab="", ylim=c(0,3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$republic, na.rm=T, bw=0.1), xlab="", ylim=c(0,3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topright", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Marital status
plot(density(df[df$exp_group==1,]$married, na.rm=T, bw=0.1), xlab="Married (0 = No, 1 = Yes)", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="Marital status", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$married, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$married, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$married, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$married, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$married, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$married, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$married, na.rm=T, bw=0.1), xlab="", ylim=c(0,3.3), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topright", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Employment status
plot(density(df[df$exp_group==1,]$unemployed, na.rm=T, bw=0.1), xlab="Unemployed (0 = No, 1 = Yes)", ylim=c(0,4), xlim=c(-0.4,1.4), main="Employment status", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$unemployed, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$unemployed, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$unemployed, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$unemployed, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$unemployed, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$unemployed, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$unemployed, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topright", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

# Union membership
plot(density(df[df$exp_group==1,]$union, na.rm=T, bw=0.1), xlab="Union member (0 = No, 1 = Yes)", ylim=c(0,4), xlim=c(-0.4,1.4), main="Union membership", lty=1, cex=1.2)
par(new=TRUE)
plot(density(df[df$exp_group==2,]$union, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=2)
par(new=TRUE)
plot(density(df[df$exp_group==3,]$union, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=3)
par(new=TRUE)
plot(density(df[df$exp_group==4,]$union, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=4)
par(new=TRUE)
plot(density(df[df$exp_group==5,]$union, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=5)
par(new=TRUE)
plot(density(df[df$exp_group==6,]$union, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=6)
par(new=TRUE)
plot(density(df[df$exp_group==7,]$union, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=7)
par(new=TRUE)
plot(density(df[df$exp_group==8,]$union, na.rm=T, bw=0.1), xlab="", ylim=c(0,4), xlim=c(-0.4,1.4), main="", axes=F, ylab="", lty=8)
par(new=TRUE, family="Times")
legend("topright", legend=c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7", "Group 8"), lty=c(1,2,3,4,5,6,7,8), cex=1, bty="n")

dev.off()
